package com.itheima.controller;

import com.itheima.domian.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 杰哥不要啦~
 * @date 2021/9/17
 */

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;
    public Result save(@RequestBody Book book){
        boolean flag = bookService.save(book);
        return new Result(flag ? true :false ,flag);

    }
    @PutMapping
    public Result update(@RequestBody Book book) {
        boolean flag = bookService.update(book);
        return new Result(false ? true:false,flag);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean falg = bookService.delete(id);
        return new Result(false ?true:false,falg);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book = bookService.getById(id);
        Boolean code =  book != null ? true : false;
        String msg = book!= null ? " ":"数据查询失败请稍后再试";
        return new Result(code,book,msg);
    }

    @GetMapping
    public Result getAll() {
        List<Book> bookList = bookService.getAll();
        Boolean code = bookList !=null ? true :false;
        String msg = bookList !=null ? "" : "数据查询失败请稍后再试";
        return new Result(code,bookList,msg);
    }
}
